import json
import re

def read_json(filename):
    with open(filename, "r", encoding="utf8") as f:
        data = json.load(f)
    return data


def write_json(filename, data):
    with open(filename, "w", encoding="utf8") as f:
        json.dump(data, f, ensure_ascii=False)


def json_name_deal(filename:str, message:list):
    filename = filename.split(".")[0]
    return {k: v for k, v in zip(message, filename.split("_"))}

def response_deal(response:str):
    # 如果有思考，则删去思考
    if response.startswith("<think>"):
        response = response.split("</think>")[1].strip()
    # 如果是```json开头的，则删去```json和```
    if response.startswith("```json"):
        response = response[7:].split("```")[0].strip()
    result = {}
    try:
        result = json.loads(response)
    except:
        print(response)
        raise ValueError("response is not json")
    return result